<!--{template lyear:header_simple_start}-->
<link rel="stylesheet" href="static/lyear/js/layui/css/layui.css?{VERHASH}">
<style>
	.icon {
		width: 30px;
		height: 30px;
	}
	.iconFirstWord {
		width: 18px;
		height: 18px;
		line-height: 16px;
		font-size: 12px;
	}
</style>
<!--{template lyear:header_simple_end}-->
<main class="bs-main-container">
	<div class="container-fluid">
		<div class="card">
			<div class="card-body">
				<div class="card-search mb-2-5">
					  <div class="row">
						<div class="col-md-8">
							<div class="input-group mb-3">
								<span class="input-group-text">{lang share_title}</span>
								<input type="text" class="form-control" name="keyword" id="keyword" placeholder="{lang share_title}" aria-label="{lang share_title}">
								<!--{if $_G['adminid']}--><span class="input-group-text">{lang username}</span>
								<input type="text" class="form-control" name="username" id="username" placeholder="{lang username}" placeholder="{lang username}" aria-label="{lang username}"><!--{/if}-->
							  </div>
						</div>
						<div class="col-md-4">
						  <button type="submit" class="btn btn-primary me-1" id="search-btn">搜索</button>
						  <button type="reset" class="btn btn-default" id="refreshsearch">重置</button>
						</div>
					  </div>
				  </div>
                <table class="layui-hide" id="table" lay-filter="table"></table>
				<script type="text/html" id="toolbar">
					<div class="layui-btn-container">
						<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
						<button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">{lang del_share}</button>
						<!--{if $_G['adminid']}--><button type="button" class="layui-btn layui-bg-orange layui-btn-sm" lay-event="forbidden">{lang screen_share}</button>
						<button type="button" class="layui-btn layui-btn-sm" lay-event="allow">{lang cancel_shielding}</button><!--{/if}-->
						<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="get">刷新</button>
					</div>
				</script>
				<script type="text/html" id="barDemo">
					<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">更多</a>
					<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="copy">复制信息</a>
				</script>
			</div>
		</div>
	</div>
</main>
<script src="static/lyear/js/layui/layui.js?{VERHASH}"></script>
<script>
	var type = '';
	layui.use('table', function(){
	  var table = layui.table;
	table.render({
	elem: '#table',
	url: '{MOD_URL}&do=getinfo',
	title: '分享管理数据表格',
	toolbar: '#toolbar',
	autoSort: false,
	even:true,
	cellMinWidth:120,
	text: {
		none: '暂无相关数据'
	},
	page: {
		limit: 20,
		limits: [10, 20, 50, 100, 200, 500, 1000,2000,5000]
	},
	cols: [[
	{type:'checkbox', fixed: 'left'}
	,{title: 'ID',width:80, fixed: 'left',type: 'numbers'}
		,{field:'title', title: '{lang share_typename_attach}', sort: true,templet: function(d) {
			return d.title;
		}}
		,{field:'status', title: '{lang status}', align: 'center', sort: true,templet: function(d) {
			return d.status;
		}}
		,{field:'dateline', title: '{lang share_time}', align: 'center', sort: true,templet:function(d){return d.dateline}}
		,{field:'endtime', title: '{lang failure_time}', align: 'center', sort: true}
		<!--{if $_G['adminid']}-->,{field:'username', title: '{lang share_user}', align: 'center', sort: true,templet: function(d) {
		return d.username;
	}}<!--{/if}-->
		,{field:'count', title: '{lang view_count}', align: 'center', sort: true,templet:function(d){return d.count+'{lang degree}'}}
		,{field:'downs', title: '{lang download_times}', align: 'center', sort: true,templet:function(d){return d.downs+'{lang degree}'}}
		,{field:'password', title: '{lang extract_password}', align: 'center', sort: true}
		,{field:'type', title: '{lang file_type}', align: 'center', sort: true}
		,{field:'times', title: '{lang limited_number}', align: 'center', sort: true}
		,{title: '{lang operation}',width:150, align:'center', toolbar: '#barDemo', fixed: 'right'}
	]],
	initSort: {
		field: 'dateline', // 默认排序字段
		type: 'desc' // 默认排序方式
	}
	});
	//头工具栏事件
	table.on('toolbar(table)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
		var data = checkStatus.dataCache;
		switch(obj.event){
			case 'getCheckData':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				// 创建模板字符串
				var template = '<div class="accordion">';
					data.forEach(function(item) {
						template += '<div class="accordion-item">';
						template += '<h2 class="accordion-header" id="Open-heading' + item.LAY_NUM + '">';
						template += '<button class="accordion-button" type="button" data-toggle="collapse" data-target="#Open-collapse' + item.LAY_NUM + '" aria-expanded="true" aria-controls="Open-collapse' + item.LAY_NUM + '">';
						template += '{lang share_typename_attach}：' + item.title;
						template += '</button>';
						template += '</h2>';
						template += '<div id="Open-collapse' + item.LAY_NUM + '" class="accordion-collapse collapse show" aria-labelledby="Open-heading' + item.LAY_NUM + '">';
						template += '<div class="accordion-body">';
						template += '<p><strong>ID:</strong> ' + item.LAY_NUM + '</p>';
						template += '<p><strong>{lang share_typename_attach}:</strong> ' + item.title + '</p>';
						template += '<p><strong>状态:</strong> ' + item.status + '</p>';
						template += '<p><strong>{lang share_time}:</strong> ' + item.dateline + '</p>';
						<!--{if $_G['adminid']}-->
						template += '<p><strong>{lang share_user}:</strong> ' + item.username + '</p>';
						<!--{/if}-->
							template += '<p><strong>{lang link}:</strong> <a href="' + item.sharelink + '" target="_blank">' + item.sharelink + '</a></p>';
							template += '<p><strong>{lang extract_password}:</strong> ' + item.password + '</p>';
							template += '<p><strong>{lang failure_time}:</strong> ' + item.endtime + '</p>';
							template += '<p><strong>{lang view_count}:</strong> ' + item.count + '{lang degree}</p>';
							template += '<p><strong>{lang download_times}:</strong> ' + item.downs + '{lang degree}</p>';
						template += '<p><strong>{lang limited_number}:</strong> ' + item.times + '</p>';
						template += '<p><strong>{lang file_type}:</strong> ' + item.type + '</p>';
						template += '</div>';
						template += '</div>';
						template += '</div>';
					});
				template += '</div>';
				layer.alert(template, {
					title: '选中的分享信息'
				});
				break;
				case 'delete':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var sids = new Array();
				$.each(data,function(i) {
					sids.push(this.sid);
				});
				layer.confirm('{lang share_stop_sharing1}', {title: '{lang affirm_stop_sharing}',skin:'lyear-skin-danger'}, function(index){
					var loading = layer.load(0, { shade:  [0.1, '#000'] });
					jQuery.post('{MOD_URL}&op=ajax&do=delete', {'sids': sids},function(json){
						layer.close(loading);
						layer.close(index);
						if(json.success){
							showmessage('删除成功', 'success', '3000', 1);
						} else if(json.error){
							showmessage(json.error, 'danger', '3000', 1);
						} else {
							showmessage('{lang error}', 'danger', '3000', 1);
						}
						table.reloadData('table');
					},'json').fail(function() {
							layer.close(loading);
							layer.close(index);
							showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
						});
				});
				break;
				case 'forbidden':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var sids = new Array();
				$.each(data,function(i) {
					sids.push(this.sid);
				});
				layer.confirm('{lang share_stop_sharing2}', {title: '{lang affirm_screen_share}',skin:'lyear-skin-warning'}, function(index){
					var loading = layer.load(0, { shade:  [0.1, '#000'] });
					jQuery.post('{MOD_URL}&op=ajax&do=forbidden', {'sids': sids, "flag": 'forbidden'},function(json){
						layer.close(loading);
						layer.close(index);
						if(json.msg){
							showmessage(json.msg, 'success', '3000', 1);
						} else if(json.error){
							showmessage(json.error, 'danger', '3000', 1);
						} else {
							showmessage('{lang shielding_failure}', 'danger', '3000', 1);
						}
						table.reloadData('table');
					},'json').fail(function() {
							layer.close(loading);
							layer.close(index);
							showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
						});
				});
				break;
				case 'allow':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var sids = new Array();
				$.each(data,function(i) {
					sids.push(this.sid);
				});
				layer.confirm('{lang share_stop_sharing3}', {title: '{lang affirm_cancel_shielding}',skin:'lyear-skin-info'}, function(index){
					var loading = layer.load(0, { shade:  [0.1, '#000'] });
					jQuery.post('{MOD_URL}&op=ajax&do=forbidden', {'sids': sids, "flag": 'allow'},function(json){
						layer.close(loading);
						layer.close(index);
						if(json.msg){
							showmessage(json.msg, 'success', '3000', 1);
						} else if(json.error){
								showmessage(json.error, 'danger', '3000', 1);
						} else {
							showmessage('{lang cancel_shielding_failure}', 'danger', '3000', 1);
						}
						table.reloadData('table');
					},'json').fail(function() {
							layer.close(loading);
							layer.close(index);
							showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
						});
				});
				break;
				case 'get':
				table.reloadData('table', {
					where: {type: type}, // 清空搜索参数
					page: {
						curr: 1 //重新从第 1 页开始
					}
				});
				break; 
			};
		});
	  //监听工具条
	  table.on('tool(table)', function(obj){
		var data = obj.dataCache;
		if(obj.event === 'detail'){
			showDataTemplate(obj);
		} else if(obj.event === 'copy'){
			copyShareInfo(data);
		}
	  });
	  //触发排序事件 
	table.on('sort(table)', function(obj){
		table.reloadData('table', {
			initSort: obj
			,where: {
				field: obj.field
				,order: obj.type
			}
		});
	});
	  table.on('rowDouble(table)', function(obj){
		showDataTemplate(obj);
	  });
		function showDataTemplate(obj) {
			var data = obj.dataCache;
			var template =
			'<p><strong>{lang share_typename_attach}:</strong> ' + data.title + '</p>' +
			'<p><strong>状态:</strong> ' + data.status + '</p>' +
			'<p><strong>{lang share_time}:</strong> ' + data.dateline + '</p>' +
			'<p><strong>{lang failure_time}:</strong> ' + data.endtime + '</p>' +
			<!--{if $_G['adminid']}-->
			'<p><strong>{lang share_user}:</strong> ' + data.username + '</p>' + 
			<!--{/if}-->
			'<p><strong>{lang link}:</strong> <a href="' + data.sharelink + '">' + data.sharelink + '</a></p>' +
			'<p><strong>{lang extract_password}:</strong> ' + data.password + '</p>' +
			'<p><strong>{lang view_count}:</strong> ' + data.count + '{lang degree}</p>' +
			'<p><strong>{lang download_times}:</strong> ' + data.downs + '{lang degree}</p>' +
			'<p><strong>{lang file_type}:</strong> ' + data.type + '</p><p><strong>{lang limited_number}:</strong> ' + data.times + '</p>';
	
			layer.alert(template, {
				title: '<strong>ID：'+data.LAY_NUM+'</strong>详情：'
			});
	
		// 标注选中样式
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
		}
		function copyShareInfo(data) {
			// 创建临时的 input 元素用于复制链接
			var tempInput = document.createElement("input");
			document.body.appendChild(tempInput);
			var copyText = data.name + "\n " + data.sharelink;
			if (data.password) {
				copyText += " \n密码: " + data.password;
			}
			tempInput.value = copyText;
			tempInput.select();
			document.execCommand("copy");
			document.body.removeChild(tempInput);
			layer.msg('信息已复制到剪贴板');
		}
		// 搜索按钮功能
		$(document).on('click', '#search-btn', function() {
		// 获取搜索关键词
		var keyword = $('#keyword').val();
		var username = $('#username').val();
		// 刷新表格并传递搜索参数
		table.reloadData('table', {
		  where: { keyword: keyword,username:username},
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	   // 重置按钮功能
	   $('#refreshsearch').click(function() {
		// 清空表单内容
        $('#username').val('');
        $('#keyword').val('');
		// 刷新表格
		table.reloadData('table', {
		  where: {type: type}, // 清空搜索参数
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	  // 类型按钮功能
	  $(document).on('click', '.type-btn', function() {
		// 移除所有 .type-btn 的 active 类
		$('.type-btn').removeClass('active');
		// 为当前点击的按钮添加 active 类
		$(this).addClass('active');
		$('#keyword').val('');
		$('#username').val('');
		// 获取当前的 type 值
		operation = $(this).data('operation');
		type = $(this).data('type');
		// 刷新表格并传递 operation 参数
		table.reloadData('table', {
		  where: {type: type},
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	});
</script>
<!--{template lyear:footer_simple}-->